package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 平台属性CRUD
 */
@Slf4j
@RequestMapping("/admin/product")
@RestController
public class BaseAttrInfoController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;


    /**
     * 查询分类下的平台属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>>  attrInfoList(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id
    ){


        List<BaseAttrInfo>  baseAttrInfos =  baseAttrInfoService.attrInfoList(category1Id,category2Id,category3Id);

        return Result.ok(baseAttrInfos);
    }


    /**
     * 保存平台属性名和值
     *
     * saveAttrInfo(BaseAttrInfo baseAttrInfo)：表单提交的k=v&k=v映射到 BaseAttrInfo、
     * saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo)：前端提交的json封装映射到BaseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        log.info("前端提交的数据：{}",baseAttrInfo);


       baseAttrInfoService.saveOrUpdateInfoAndValue(baseAttrInfo);
        return Result.ok();
    }


    /**
     * getAttrValueList
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId){

        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attrId);
        List<BaseAttrValue> list = baseAttrValueService.list(wrapper);

        return Result.ok(list);
    }
}
